/**************************************************************************
Writen by: Cedric Snoy
Started: 5/1/12
Location: The Master version is located on the zero-defect-go-parser git
    repository in the scr directory.
Reason: to implement the functions that handle the list of board positions
    in the zero defect go reviewer program.
Context: Serveral other source files are include in this project including
    reader, board and main files. All of which are locked in the same
    directory as this file.
**************************************************************************/
#include <stdio.h>//included for printf fuction
#include <stdlib.h>//included for malloc function

#include "state.h"
#include "board.h"//included for the printboard fuction
/**
 * Takes a position and returns the next position.
 *[let size be a constent defined in board.h->
 * if position!=last position->
 *     stdout:=stdout + size lines each containing a row of
 *     position->next->board's position array with a space between
 *     each element + a line containing position->next->board's
 *     message.
 * else->
 *     stdout:=stdout + statement that there is no next move
 *     return position]
 */
Position * nextPosition(Position * position)
{
    /*-1*/
    if(position->next!=position)
    {
        /*-1.1
         *[let size be a constent defined in board.h->
         * stdout:=stdout + size lines each containing a row of
         *     position->next->board's position array with a space between
         *     each element + a line containing position->next->board's
         *     message.
         * return position->next]
         */
        printBoard(positions->next->board);
        return(position->next);
    }
    else
    {
        /*-1.2
         *[stdout:=stdout + statement that there is no next move
         * return position]
         */
        printf("There were no moves after this one.\n");
        return(position);
    }
} 

/**
 *[let size be a constent defined in board.h->
 * if position!=first Position->
 *     stdout:=stdout + size lines each containing a row of
 *     position->prev->board's position array with a space between
 *     each element + a line containing position->prev->board's
 *     message.
 *     return position->prev
 * else->
 *     stdout:=stdout + statement that there is no previous move
 *     return position]
 */
Position * prevPosition(Position * position)
{
    /*-1*/
    if(position->prev!=position)
    {
        /*-1.1
         *[let size be a constent defined in board.h->
         * stdout:=stdout + size lines each containing a row of
         *     position->prev->board's position array with a space between
         *     each element + a line containing position->prev->board's
         *     message.
         * return position->prev]
         */
        printBoard(positions->prev->board);
        return(position->prev);
    }
    else
    {
        /*-1.2
         *[stdout:=stdout + statement that there is no next move
         * return position]
         */
        printf("There were no moves befor this one.\n");
        return(position);
    }
} 

/** 
 * Adds the next position to the current position. 
 * [tail's next position = next
 *  next's prev position = tail] 
 **/
void addPosition(Position * tail, Position * next)
{
    /*-1*/
    tail->next=next;
    next->prev=tail;
}

/**
 * Creates a position that has itself as a next and prev position.
 *[pos->next:=pos
 * pos->prev:=pos
 * pos->board:=board
 * return pos]
 */
Position * createPosition(Board * board)
{
    /*-1
     *[*pos:=allocated space
     * pos:=pointer to *pos]
     */
    Position pos = (Position*) malloc(sizeof(Position));
    /*-2*/
    pos->next=pos;
    pos->prev=pos;
    pos->board=board;
    return(pos);
}
